package com.ott.Util;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

public class MtsHttpClient {
	
	/**
	 * 发送 post请求访问本地应用并根据传递参数不同返回不同结果
	 */
	
	public static String post(String url,String data){
		String result = "";
		try{
			DefaultHttpClient httpClient = new DefaultHttpClient();
			httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5 * 60 * 1000);
			httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5 * 60 * 1000);
//			logger.info("request url:" + url);
			HttpPost method = new HttpPost(url);
//			logger.info("request data:" + data);
			StringEntity entity = new StringEntity(data,"UTF-8");//解决中文乱码问题  
			entity.setContentEncoding("UTF-8");  
			entity.setContentType("application/json");  
			method.setEntity(entity);
			try {
				HttpResponse response = httpClient.execute(method);
				int stateCode = response.getStatusLine().getStatusCode();
				if(stateCode != 200){
					return result;
				}
				result = EntityUtils.toString(response.getEntity(),"UTF-8");
			} catch (ClientProtocolException e) {
			} catch (ParseException e) {
			} catch (IOException e) {
			}finally {
				if (method != null) {
					method.releaseConnection();
	            }
	            if (httpClient != null) {
	            	//httpClient.getConnectionManager().shutdown();
	            	httpClient.close();
	            }
			}
		}catch (Exception e){
		}
//		logger.info("request result:" + result);
		return result;
	}
}